package com.twetec.cms.adapter.pc.backend.controller.website;

import com.twetec.cms.adapter.pc.backend.response.R;
import com.twetec.cms.adapter.pc.backend.response.ResultCodeMsgEnum;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.vo.website.SiteClusterServerPageListVO;
import com.twetec.cms.model.vo.website.SiteClusterServerVO;
import com.twetec.cms.service.website.SiteClusterServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

@Api(tags = "站点集群(已废弃)")
@RestController
@RequestMapping("/system/site-cluster")
public class SiteClusterController {

    @Autowired
    private SiteClusterServerService siteClusterServerService;

    @ApiOperation(value = "服务器分页查询列表", notes = "服务器分页查询列表")
    @GetMapping("/server-page-search-list")
    public R<SiteClusterServerPageListVO> serverPageSearchList(
            @ApiParam(value = "站点id", required = true, example = "123456789") @RequestParam(required = false) Long siteId,
            @ApiParam(value = "服务器名称", required = false, example = "服务器1") @RequestParam(required = false) String name,
            @ApiParam(value = "IP地址", required = false, example = "110.87.82.000") @RequestParam(required = false) String ip,
            @ApiParam(value = "FTP端口", required = false, example = "8080") @RequestParam(required = false) Integer ftpPort,
            @ApiParam(value = "站点根目录", required = false, example = "11111111") @RequestParam(required = false) String siteRootPath,
            @ApiParam(value = "状态 0表示启用 1表示禁用", required = false, example = "1") @RequestParam(required = false) Integer status,
            @ApiParam(value = "根据更新时间搜索的开始时间（10位时间戳）", required = false, example = "1234567891") @RequestParam(required = false) Date gmtModifiedStart,
            @ApiParam(value = "根据更新时间搜索的结束时间（10位时间戳）", required = false, example = "1231231231") @RequestParam(required = false) Date gmtModifiedEnd,
            @ApiParam(value = "更新人", required = false, example = "admin") @RequestParam(required = false) String modifierName,
            @ApiParam(value = "备注", required = false, example = "admin") @RequestParam(required = false) String remark,
            @ApiParam(value = "访问页码", required = true, example = "1") @RequestParam(required = false) Integer page,
            @ApiParam(value = "页面容量", required = true, example = "10") @RequestParam(required = false) Integer limit) {


        // 校验访问页码
        if (page == null || !ValidateUtil.Number(page) || page <= 0) { return R.failureRequestParam().msg("访问页码必填"); }
        // 校验访问数据条数
        if (limit == null || !ValidateUtil.Number(limit) || limit <= 0) { return R.failureRequestParam().msg("页面容量必填"); }
        // 检验站点id
        if (siteId == null || !ValidateUtil.Number(siteId)) return R.failureRequestParam().msg("站点id不能为空");

        // 调用service    creatorName, gmtSetStart, gmtSetEnd,
        SiteClusterServerPageListVO siteClusterServerPageListVO = siteClusterServerService.pageSearchList(siteId, name, ip, ftpPort, siteRootPath, status, gmtModifiedStart, gmtModifiedEnd, modifierName, remark, page, limit);
            return R.success().data(siteClusterServerPageListVO);
    }

    @ApiOperation(value = "新增服务器", notes = "新增服务器")
    @PostMapping("/create-server")
    public R createServer(
            @ApiParam(value = "站点id", required = true, example = "123456789") @RequestParam(required = false) Long siteId,
            @ApiParam(value = "服务器名称", required = true, example = "服务器1") @RequestParam(required = false) String name,
            @ApiParam(value = "IP地址", required = true, example = "110.87.82.000") @RequestParam(required = false) String ip,
            @ApiParam(value = "FTP端口", required = true, example = "8080") @RequestParam(required = false) Integer ftpPort,
            @ApiParam(value = "站点根目录", required = true, example = "11111111") @RequestParam(required = false) String siteRootPath,
            @ApiParam(value = "状态 0表示启用 1表示禁用", required = true, example = "1") @RequestParam(required = false) Integer status,
            @ApiParam(value = "备注", required = false, example = "新增服务器的备注") @RequestParam(required = false) String remark,
            @RequestAttribute("userId") Long userId) {


        // 校验服务器名称
        if (name == null || StringUtils.isEmpty(name)) { return R.failureRequestParam().msg("服务器名称必填"); }
        // 校验IP地址
        if (ip == null || !ValidateUtil.IP4(ip)) { return R.failureRequestParam().msg("IP地址输入有误"); }
        // 校验FTP端口
        if (ftpPort == null || !ValidateUtil.Number(ftpPort) || ftpPort < 0) { return R.failureRequestParam().msg("FTP端口必填"); }
        // 校验站点根目录
        if (siteRootPath == null || StringUtils.isEmpty(siteRootPath)) { return R.failureRequestParam().msg("站点根目录必填"); }
        // 校验状态
        if (status == null || !ValidateUtil.Number(status)) { return R.failureRequestParam().msg("服务器状态必填"); }
        // 检验站点id
        if (siteId == null || !ValidateUtil.Number(siteId)) return R.failureRequestParam().msg("站点id不能为空");


         Boolean result = siteClusterServerService.saveSiteClusterServer(siteId, name, ip, ftpPort, siteRootPath, status, remark, userId);
         if (result) return R.success().msg("新增服务器成功");
        return R.failure().msg("新增服务器失败");
    }

    @ApiOperation(value = "批量删除服务器", notes = "批量删除服务器")
    @DeleteMapping("/delete-server-by-ids")
    public R deleteServerByIds(@ApiParam(value = "ids", required = true, example = "12312313123") @RequestParam(required = false) String ids,
                               @RequestAttribute("userId") Long userId) {

        // 校验id
        if (ids == null || StringUtils.isEmpty(ids)) { return R.failureRequestParam().msg("请指定要删除的服务器id"); }
        String[] idSplit = ids.split(",");
        ArrayList<Long> idList = new ArrayList<>();
        for (String idString : idSplit) {
            Long id = Long.valueOf(idString);
            idList.add(id);
        }

        Boolean result = siteClusterServerService.removeByIdList(idList, userId);
        if (result) return R.success().msg("批量删除成功");
        return R.failure().msg("批量删除失败");
    }

    @ApiOperation(value = "根据id更新服务器", notes = "根据id更新服务器")
    @PutMapping("/update-server-by-id")
    public R updateServerById(
            @ApiParam(value = "id", required = true, example = "12312313123") @RequestParam(required = false) Long id,
            @ApiParam(value = "服务器名称", required = false, example = "服务器1") @RequestParam(required = false) String name,
            @ApiParam(value = "IP地址", required = false, example = "110.87.82.000") @RequestParam(required = false) String ip,
            @ApiParam(value = "FTP端口", required = false, example = "8080") @RequestParam(required = false) Integer ftpPort,
            @ApiParam(value = "站点根目录", required = false, example = "11111111") @RequestParam(required = false) String siteRootPath,
            @ApiParam(value = "状态 0表示启用 1表示禁用", required = false, example = "1") @RequestParam(required = false) Integer status,
            @ApiParam(value = "备注", required = false, example = "新增服务器的备注") @RequestParam(required = false) String remark,
            @RequestAttribute("userId") Long userId) {

        // 校验id
        if (id == null || !ValidateUtil.Number(id)) { return R.failureRequestParam().msg("id输入有误"); }

        // (Long id, String name, String ip, Integer ftpPort, String siteRootPath, Integer status, String remark, Long modifierId);
        Boolean result = siteClusterServerService.updateSiteClusterServer(id, name, ip, ftpPort, siteRootPath, status, remark, userId);

        if (result) return R.success().msg("更新成功");
        return R.failure().msg("更新失败");
    }

    @ApiOperation(value = "根据id更新服务器状态", notes = "更新服务器状态")
    @PutMapping("/update-server-status-by-id")
    public R updateServerStatusById(
            @ApiParam(value = "id", required = true, example = "12312313123") @RequestParam(required = false) Long id,
            @ApiParam(value = "状态 0表示启用 1表示禁用", required = true, example = "1") @RequestParam(required = false) Integer status,
            @RequestAttribute("userId") Long userId) {

        // 校验id
        if (id == null || !ValidateUtil.Number(id))  return R.failureRequestParam().msg("id输入有误");

        Boolean result = siteClusterServerService.updateStatusById(id, status, userId);
        if (result) return R.success().msg("更新状态成功");
        return R.failure().msg("更新状态失败");
    }

    @ApiOperation(value = "根据id删除服务器", notes = "根据id删除服务器")
    @DeleteMapping("/delete-server-by-id")
    public R deleteServerById(
            @ApiParam(value = "id", required = true, example = "12312313123") @RequestParam(required = false) Long id,
            @RequestAttribute("userId") Long userId) {

        // 校验id
        if (id == null || !ValidateUtil.Number(id)) { return R.failureRequestParam().msg("id输入有误"); }

        Boolean result = siteClusterServerService.removeSiteClusterServerById(id, userId);
        if (result) return R.success().msg("删除服务器成功");
        return R.failure().msg("删除服务器失败");
    }

    @ApiOperation(value = "根据id获取服务器信息", notes = "根据id获取服务器信息")
    @GetMapping("/get-server-by-id")
    public R<SiteClusterServerVO> getServerById(
            @ApiParam(value = "id", required = true, example = "12312313123") @RequestParam(required = false) Long id) {

        // 校验id
        if (id == null || !ValidateUtil.Number(id)) { return R.failureRequestParam().msg("id输入有误"); }

        SiteClusterServerVO siteClusterServerVO = siteClusterServerService.getSiteClusterById(id);
        if (siteClusterServerVO != null) {
            return R.success().data(siteClusterServerVO);
        }

        return R.resultCodeMsg(ResultCodeMsgEnum.DATA_NOT_EXIST);
    }

}